package com.thrc.borrow.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.thrc.borrow.dao.RoleDao;
import com.thrc.borrow.entity.Role;
import com.thrc.borrow.vo.RoleVo;

@Service
public class RoleService implements BaseService<Role, RoleVo> {

	@Autowired
	private RoleDao roleDao;

	@Override
	public RoleVo selectById(Role t) {
		return roleDao.selectById(t);
	}

	@Override
	public List<RoleVo> selectAll(Role t) {
		return roleDao.selectAll(t);
	}

	@Override
	public int selectCount(Role t) {
		return roleDao.selectCount(t);
	}

	@Override
	public List<RoleVo> selectByPage(Role t) {
		t.setStartRow(t.getPageIndex(), t.getPageSize());
		return roleDao.selectByPage(t);
	}

	@Override
	public void update(Role t) {
		roleDao.update(t);
		roleDao.deleteRoleMenu(t);
		if (StringUtils.isEmpty(t.getMenuArrStr()))
			return;
		t.setMenuList(Arrays.asList(t.getMenuArrStr().split(",")));
		roleDao.insertRoleMenu(t);
	}

	@Override
	public void delete(Role t) {
		roleDao.delete(t);
		roleDao.deleteRoleMenu(t);
	}

	@Override
	public void insert(Role t) {
		roleDao.insert(t);
		if (StringUtils.isEmpty(t.getMenuArrStr()))
			return;
		t.setMenuList(Arrays.asList(t.getMenuArrStr().split(",")));
		roleDao.insertRoleMenu(t);
	}

	public String getMenuArrByRole(Role t) {
		List<String> list = roleDao.getMenuListByRole(t);
		if (list == null || list.size() == 0)
			return "";
		StringBuilder retSb = new StringBuilder();
		for (String id : list) {
			retSb.append(id).append(",");
		}
		return retSb.toString();
	}

}
